package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogComment;
import com.ruoyi.blog.service.IBlogCommentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excel.ExcelUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 评论管理Controller
 *
 * @author 刘哲贤
 * @date 2022-08-30
 */
@RestController
@RequestMapping("/admin/comment")
public class BlogCommentController extends BaseController {
    @Resource
    private IBlogCommentService blogCommentService;

    /**
     * 查询评论管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogComment blogComment) {
        startPage();
        List<BlogComment> list = blogCommentService.selectBlogCommentList(blogComment);
        return getDataTable(list);
    }

    /**
     * 导出评论管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:comment:export')")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogComment blogComment) {
        List<BlogComment> list = blogCommentService.selectBlogCommentList(blogComment);
        ExcelUtils<BlogComment> util = new ExcelUtils<BlogComment>(BlogComment.class);
        util.exportData(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(blogCommentService.selectBlogCommentById(id));
    }

    /**
     * 新增评论管理
     */
    @PreAuthorize("@ss.hasPermi('admin:comment:add')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogComment blogComment) {
        blogComment.setCreateTime(new Date());
        return toAjax(blogCommentService.insertBlogComment(blogComment));
    }

    /**
     * 修改评论管理
     */
    @PreAuthorize("@ss.hasPermi('admin:comment:edit')")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogComment blogComment) {
        return toAjax(blogCommentService.updateBlogComment(blogComment));
    }

    /**
     * 删除评论管理
     */
    @PreAuthorize("@ss.hasPermi('admin:comment:remove')")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blogCommentService.deleteBlogCommentByIds(ids));
    }
}
